<template>
  <transition name="dialog-fade">
    <div class="t-dialog__wrapper" v-show="innerVisible" @click.self="handleWrapperClick">
      <div class="t-dialog" :class="size ? 't-dialog--' + size : ''" ref="dialog">
        <div class="t-dialog__header" v-if="$slots.title">
          <slot name="title">
            <span>{{ title }}</span>
          </slot>
          <button type="button" class="t-dialog__header__btn-close" aria-label="close" v-show="showCloseBtn" @click="handleClose"><i class="iconfont icon-guanbi"></i></button>
        </div>
        <div class="t-dialog__body">
          <slot name="content"></slot>
        </div>
        <div class="t-dialog__footer" v-show="$slots.footer">
          <slot name="footer"></slot>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  export default {
    name: 'TDialog',
    props: {
      title: {
        type: String,
        default: '',
      },
      visible: {
        type: Boolean,
        default: false,
      },
      size: {
        type: String,
        default: 'small',
      },
      top: {
        type: String,
        default: '15%',
      },
      showCloseBtn: {
        type: Boolean,
        default: true,
      },
      closeOnClickWrapper: {
        type: Boolean,
        default: true,
      },
    },
    data() {
      return {
        store: {
          innerVisible: false,
          switchInnerVisible() {
            this.innerVisible = !this.innerVisible;
          },
        },
      };
    },
    computed: {
      style() {
        return this.size === 'full' ? {} : {
          top: this.top,
        };
      },
      innerVisible() {
        return this.store.innerVisible;
      },
    },
    watch: {
      visible() {
        this.handleSwitchVisible();
      },
    },
    methods: {
      handleSwitchInnerVisible() {
        this.store.switchInnerVisible();
      },
      handleClose() {
        this.$emit('dialog-close');
      },
      handleSwitchVisible() {
        this.handleSwitchInnerVisible();
      },
      handleWrapperClick() {
        this.handleClose();
      },
    },
  };
</script>